package com.hz.ruoyiequipment.equipment.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentFertigation;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentSf;
import com.hz.ruoyiequipment.equipment.domain.AgrFertigationData;
import com.hz.ruoyiequipment.equipment.domain.vo.WafEquipment;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


/**
 * @author pby
 * @date 2025/3/8 17:27
 * @description:  mapper
 */
public interface WafEquipmentMapper extends BaseMapper<WafEquipment> {

    List<AgrEquipmentFertigation> listPlotsStatus();

    List<WafEquipment> getWafEquipment(
            @Param("equipmentStatus") Integer equipmentStatus,
            @Param("equipmentCode") String equipmentCode,
            @Param("plotsName") String plotsName);

    // 根据equipmentId查询设备历史信息
    WafEquipment getWafDetail(Long equipmentId);
     Integer getSfStatusByEquipmentIdInner(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 11:04
     * @Description: 新增水肥设备到设备表
     */
    @Insert("INSERT INTO agr_equipment (equipment_code, equipment_name, equipment_brand, equipment_type, equipment_img)" +
            "VALUE (#{equipmentCode}, #{equipmentName}, #{equipmentBrand}, #{equipmentType}, #{equipmentImg})")
    @Options(useGeneratedKeys = true, keyProperty = "equipmentId")
    Integer insertEquipment(AgrEquipment agrEquipment);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 14:10
     * @Description: 新增水肥设备到水肥设备表
     */
    @Insert("INSERT INTO agr_equipment_sf (equipment_id, sf_channel, sf_pump, sf_input, sf_flow_rate, sf_control, sf_mixing, sf_control_mode, sf_interface, sf_protection)" +
            "VALUE (#{equipmentId}, #{sfChannel}, #{sfPump}, #{sfInput}, #{sfFlowRate}, #{sfControl}, #{sfMixing}, #{sfControlMode}, #{sfInterface}, #{sfProtection})")
    Integer insertWafEquipment(AgrEquipmentSf agrEquipmentSf);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 14:12
     * @Description: 将设备绑定的地块信息插入到设备地块表中
     */
    @Insert("INSERT INTO agr_equipment_plots (equipment_id, plots_id)" +
            "VALUE (#{equipmentId}, #{plotsId})")
    Integer insertEquipmentPlots(WafEquipment wafEquipment);

    @Update("UPDATE agr_equipment SET equipment_code = #{equipmentCode}, equipment_name = #{equipmentName}, equipment_brand = #{equipmentBrand}, equipment_status = #{equipmentStatus}, equipment_type = #{equipmentType}, equipment_img = #{equipmentImg} " +
            "WHERE equipment_id = #{equipmentId}")
    Integer updateEquipment(WafEquipment wafEquipment);
    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 14:44
     * @Description: 修改水肥设备表中的数据
     */
    @Update("UPDATE agr_equipment_sf SET sf_channel = #{sfChannel}, sf_pump = #{sfPump}, sf_input = #{sfInput}, sf_flow_rate = #{sfFlowRate}, sf_control = #{sfControl}, sf_mixing = #{sfMixing}, sf_control_mode = #{sfControlMode}, sf_interface = #{sfInterface}, sf_protection = #{sfProtection} " +
            "WHERE equipment_id = #{equipmentId}")
    Integer updateEquipmentSf(WafEquipment wafEquipment);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 15:03
     * @Description: 删除设备表中水肥机的数据
     */
    @Delete("DELETE FROM agr_equipment WHERE equipment_id = #{equipmentId}")
    Integer delEquipmentByEquipmentId(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 15:05
     * @Description: 删除设备地块表中的数据
     */
    @Delete("DELETE FROM agr_equipment_plots WHERE equipment_id = #{equipmentId}")
    Integer delEquipmentPlotsByEquipmentId(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/10 15:05
     * @Description: 删除水肥设备表中的数据
     */
    @Delete("DELETE FROM agr_equipment_sf WHERE equipment_id = #{equipmentId}")
    Integer delEquipmentSfByEquipmentId(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/13 14:34
     * @Description: 新增水肥设备到控制信息表
     */
    @Insert("INSERT INTO agr_equipment_fertigation (equipment_id)" +
            "VALUE (#{equipmentId})")
    Integer insertAgrEquipmentFertigation(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/13 15:00
     * @Description: 删除水肥控制表的水肥机数据
     */
    @Insert("DELETE FROM agr_equipment_fertigation WHERE equipment_id = #{equipmentId}")
    Integer delAgrEquipmentFertigationByEquipmentId(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/13 15:02
     * @Description: 水肥机控制信息
     */
    @Update("UPDATE agr_equipment_fertigation SET water_injection = #{waterInjection}, fert_solution = #{fertSolution}, irrigation_time = #{irrigationTime}, main_pump = #{mainPump} " +
            "WHERE equipment_id = #{equipmentId}")
    Integer updateAgrEquipmentFertigation(AgrEquipmentFertigation agrEquipmentFertigation);

    WafEquipment getWafEquipmentID(Long equipmentId);

    /**
     * @return:
     * @Author: xqd
     * @Date: 2025/3/13 15:34
     * @Description: 修改水肥设备主泵状态
     */
    @Update("UPDATE agr_equipment_fertigation SET main_pump = #{mainPump} WHERE equipment_id = #{equipmentId}")
    Integer updateSfStatus(AgrEquipmentFertigation agrEquipmentFertigation);

    Integer insertAgrFertigationDataBatch(Map<String, Object> params);

    @Select("SELECT plots_id,record_date,inlet_pressure,instant_water,water_use,instant_ferti,fert_use FROM agr_fertigation_data WHERE equipment_id = #{equipmentId} order by record_date desc")
    List<AgrFertigationData> getAgrFertigationData(Long equipmentId);

    @Select("SELECT fert_id, equipment_id, water_injection, fert_solution, irrigation_time, main_pump FROM agr_equipment_fertigation WHERE equipment_id = #{equipmentId}")
    AgrEquipmentFertigation getAgrEquipmentFertigation(Long equipmentId);
}
